/*
 * @Description  : 数据更新与维护
 * @Author       : zhao
 * @Date         : 2022-07-26 18:56:26
 * @LastEditors  : zhao
 * @LastEditTime : 2023-01-31 17:05:26
 */

import { defineStore } from 'pinia';
import { UserInfosStates } from './interface';
import { useTableInfo } from './ztable';
import { ElMessage, ElMessageBox } from 'element-plus'
import type { Action } from 'element-plus'
import { ImportMdbFile, GetArchiveList, GetArchiveFileList, ImportXlsFile, LoadXmlFile, UpdateArchiveDataList, DeleteArchiveDataList } from '/@/api/updataMaintain.js';



export const useUpdataMaintain = defineStore('updataMaintain', {
	state: (): UserInfosStates => ({
		userInfos: {
			userName: '',
			photo: '',
			time: 0,
			roles: [],
			authBtnList: [],
		},
	}),
	actions: {


		//导入mdb文件
		importMdbFile(data: any): Promise<any> {
			return new Promise((resolve, reject) => {
				ImportMdbFile(data).then((res: any) => {
					if (res.code === 200) {
						resolve(res)
						// ElMessage.success({ message: res.msg || "导入成功", showClose: true, duration: 0 })
						ElMessageBox.alert(`${res.msg || "导入成功"} `, '消息提醒', {
							confirmButtonText: '确认',
						})
					} else {
						resolve(res)
						this.downloadTxt(res.msg)
					}

				}).catch((error: any) => {
					resolve(error)
				})

			})

		},
		//根据查询条件获取对应案卷信息
		getArchiveList(data: any): Promise<any> {
			return new Promise((resolve, reject) => {
				GetArchiveList(data).then((res: any) => {
					if (res.code === 200) {
						resolve(res)
					} else {
						reject(res)
					}

				})

			})

		},
		//根据查询条件获取对应文件信息
		getArchiveFileList(data: any): Promise<any> {
			return new Promise((resolve, reject) => {
				GetArchiveFileList(data).then((res: any) => {
					if (res.code === 200) {
						resolve(res)
					} else {
						reject(res)
					}
				})
			})
		},
		//导入xls文件
		ImportXlsFile(data: any): Promise<any> {
			return new Promise((resolve, reject) => {
				ImportXlsFile(data).then((res: any) => {
					if (res.code === 200) {
						resolve(res)
						ElMessageBox.alert(`${res.msg || "导入成功"} `, '消息提醒', {
							confirmButtonText: '确认',
						})
					} else {

						resolve(res)

						this.downloadTxt(res.msg)
					}

				}).catch((error: any) => {

					resolve(error)
				})
			})
		},
		//导入xml文件
		LoadXmlFile(data: any): Promise<any> {
			return new Promise((resolve, reject) => {
				LoadXmlFile(data).then((res: any) => {
					if (res.code === 200) {
						resolve(res)
						// ElMessage.success({ message: res.msg || "加载成功", })
					} else {

						resolve(res)
						this.downloadTxt(res.msg)

					}

				}).catch((error: any) => {

					resolve(error)
				})
			})
		},
		//更新xml档案及文件资料信息
		UpdateArchiveDataList(data: any): Promise<any> {
			return new Promise((resolve, reject) => {
				UpdateArchiveDataList(data).then((res: any) => {
					if (res.code === 200) {
						resolve(res)
						ElMessage.success(res.msg || "导入成功")
					} else {

						resolve(res)
						ElMessage.error(res.msg || "导入失败")
					}

				}).catch((error: any) => {

					resolve(error)
				})
			})
		},
		//删除 档案及文件资料信息
		DeleteArchiveDataList(data: any): Promise<any> {
			return new Promise((resolve, reject) => {
				const useTableInfos = useTableInfo()
				useTableInfos.buttonLoading = true
				DeleteArchiveDataList(data).then((res: any) => {

					if (res.code === 200) {
						resolve(res)
						ElMessage.success(res.msg || "删除成功")
					} else {

						resolve(res)
						ElMessage.error(res.msg || "删除失败")
					}

				}).catch((error: any) => {
					resolve(error)
				}).finally(() => {
					console.log("s");

					useTableInfos.buttonLoading = false

				})
			})
		},
		downloadTxt(data: any) {
			ElMessage.error("上传出错，请阅读错误日志并修改")

			var element = document.createElement('a');

			element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(data));

			element.setAttribute('download', `地质资料上传错误日志(${this.Date()})`);




			element.style.display = 'none';

			document.body.appendChild(element);



			element.click();

			document.body.removeChild(element);

		},
		Date() {
			let dateTime = null
			const nowDate = new Date();
			const date = {
				year: nowDate.getFullYear(),
				month: nowDate.getMonth() + 1,
				date: nowDate.getDate(),
				hours: nowDate.getHours(),
				minutes: nowDate.getMinutes(),
				seconds: nowDate.getSeconds(),
			};

			const newmonth = date.month > 10 ? date.month : "0" + date.month;
			const newday = date.date > 10 ? date.date : "0" + date.date;
			const newminutes = date.minutes > 10 ? date.minutes : "0" + date.minutes;
			const newseconds = date.seconds > 10 ? date.seconds : "0" + date.seconds;
			//   const newminutes = date.minutes < 10 ? "0" + date.minutes : date.minutes;
			//   const newseconds = date.seconds < 10 ? "0" + date.seconds : date.seconds;

			dateTime =
				date.year +
				"-" +
				newmonth +
				"-" +
				newday
			// + " " +
			// date.hours
			// +
			// ":" +
			// newminutes +
			// ":" +
			// newseconds;


			return dateTime
		},

	},
});
